<template>
	<el-dialog
		:title="title"
		:visible.sync="addComShow"
		width="50%"
		:before-close="cancel"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
	>
		<el-form ref="form" :model="fundManagerForm" label-width="80px" label-position="left">
			<el-form-item label="基金" required>
				<el-select
					v-model="fundManagerForm.fundSysCode"
					filterable
					clearable
					:disabled="title === '复核'"
					placeholder="请选择基金"
					@change="fundChange"
				>
					<el-option
						v-for="(item, index) in fundList"
						:key="index + item.fundSysCode"
						:label="item.fundCode + '-' + item.fundFullName"
						:value="item.fundSysCode"
					>
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="投资团队" required>
				<el-select v-model="fundManagerForm.invest" filterable clearable :disabled="title === '复核'" placeholder="请选择投资风格">
					<el-option v-for="item in productDictList" :key="item.typeCode + 'a'" :label="item.typeName" :value="item.typeCode"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="开始时间" required>
				<el-date-picker
					:disabled="title === '复核'"
					:picker-options="pickerOption1"
					type="date"
					placeholder="选择日期"
					@input="changeTime1"
					v-model="fundManagerForm.startDate"
					value-format="yyyyMMdd"
					format="yyyyMMdd"
					style="width: 100%"
				></el-date-picker>
			</el-form-item>
			<el-form-item label="结束时间">
				<el-date-picker
					:disabled="title === '复核'"
					:picker-options="pickerOption2"
					type="date"
					placeholder="选择日期"
					@input="changeTime2"
					v-model="fundManagerForm.endDate"
					value-format="yyyyMMdd"
					format="yyyyMMdd"
					style="width: 100%"
				></el-date-picker>
			</el-form-item>
			<el-form-item label="附件">
				<el-col :span="11">
					<el-upload
						class="upload-demo"
						:before-remove="beforeRemove"
						:on-exceed="handleExceed"
						:http-request="toUploadFile"
						:on-change="getValChangeyes"
						:on-preview="handlePreview"
						action=""
						:disabled="title === '复核'"
						:file-list="fileList"
					>
						<el-button :disabled="title === '复核'" size="mini" type="primary">上传文件</el-button>
					</el-upload>
				</el-col>
			</el-form-item>
			<el-form-item label=" " v-if="!!checkRemark">
				<span style="color: red">驳回备注：{{ checkRemark }}</span>
			</el-form-item>
		</el-form>

		<span slot="footer" class="dialog-footer" v-if="title === '复核'">
			<el-button size="small" @click="checkFundManager(2)">驳 回</el-button>
			<el-button size="small" type="primary" @click="checkFundManager(1)">确认无误</el-button>
		</span>
		<span slot="footer" class="dialog-footer" v-else>
			<el-button size="small" @click="cancel">取 消</el-button>
			<el-button size="small" type="primary" @click="addConfirm">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
import { addTeam, editTeam, checkTeam, ramakeTeam, appraisalFund } from '@/api/productApi/assessment'
import { uploadFileAndSort, downloadFile } from '@/api/productApi/file'
import { getFundList } from '@/api/productApi/mechanism'
export default {
	props: {
		title: {
			type: String,
			default: ''
		},
		addComShow: {
			type: Boolean,
			default: false
		},
		comIsShow: {
			type: Function,
			default: Function
		},
		queryFundManagerList: {
			type: Function,
			default: Function
		},
		productDictList: {
			type: Array,
			default: Array
		}
	},
	data() {
		return {
			fundManagerForm: {
				fundSysCode: '',
				invest: '',
				startDate: '',
				endDate: '',
				fileId: '',
				fileName: ''
			},
			fundList: [], // 考核列表（下拉框）
			time1: '',
			time2: '',
			fileList: [],
			checkRemark: '',
			time3: '',
			time4: '',
			get pickerOption1() {
				const _this = this
				return {
					disabledDate(time) {
						return (
							time.getTime() > new Date(_this.time2).getTime() ||
							time.getTime() < new Date(_this.time3).getTime() ||
							time.getTime() > new Date(_this.time4).getTime()
						)
					}
				}
			},
			get pickerOption2() {
				const _this = this
				return {
					disabledDate(time) {
						return (
							time.getTime() < new Date(_this.time1).getTime() ||
							time.getTime() < new Date(_this.time3).getTime() ||
							time.getTime() > new Date(_this.time4).getTime()
						)
					}
				}
			}
		}
	},
	mounted() {
		this.getFundList()
	},
	methods: {
		handlePreview(file) {
			downloadFile({ fid: this.fundManagerForm.fileId }).then(item => {
				if (item.msgCode === 'N') {
					this.$message({ showClose: true, message: '文件不存在', type: 'error' })
					return
				}
				if (item) {
					window.open(`/update/download.do?fid=${this.fundManagerForm.fileId}`)
				}
			})
		},
		// 文件值改变时触发 change事件
		getValChangeyes(file, fileList) {
			if (fileList.length > 0) {
				this.fileList = [fileList[fileList.length - 1]]
			} else {
				this.fileList = fileList[0]
			}
		},
		fundChange() {
			const obj = {
				fundSysCode: this.fundManagerForm.fundSysCode,
				filedCheckStatus: '1',
				fundRaiseType: '0'
			}
			appraisalFund(obj).then(res => {
				if (res.msgCode === 'Y') {
					if (!!res.data.body.productFundInfo && !!res.data.body.productFundInfo.dateInfo) {
						const establishDate = res.data.body.productFundInfo.dateInfo.establishDate // 合同生效日
						const operateEndDate = res.data.body.productFundInfo.dateInfo.operateEndDate // 运作终止日
						if (typeof establishDate !== 'undefined' && establishDate != null && establishDate !== '') {
							const i =
								establishDate.substring(0, 4) +
								'-' +
								establishDate.substring(4, 6) +
								'-' +
								establishDate.substring(6, 8) +
								' 00:00:00'
							this.time3 = new Date(i)
						}
						if (typeof operateEndDate !== 'undefined' && operateEndDate != null && operateEndDate !== '') {
							const i =
								operateEndDate.substring(0, 4) +
								'-' +
								operateEndDate.substring(4, 6) +
								'-' +
								operateEndDate.substring(6, 8) +
								' 00:00:00'
							this.time4 = new Date(i)
						}
					}
				}
			})
		},
		// 开始日期
		changeTime1() {
			if (!this.fundManagerForm.startDate) {
				this.time1 = ''
			} else {
				const i =
					this.fundManagerForm.startDate.substring(0, 4) +
					'-' +
					this.fundManagerForm.startDate.substring(4, 6) +
					'-' +
					this.fundManagerForm.startDate.substring(6, 8) +
					' 00:00:00'
				this.time1 = new Date(i)
			}
		},
		// 结束日期
		changeTime2() {
			if (!this.fundManagerForm.endDate) {
				this.time2 = ''
			} else {
				const i =
					this.fundManagerForm.endDate.substring(0, 4) +
					'-' +
					this.fundManagerForm.endDate.substring(4, 6) +
					'-' +
					this.fundManagerForm.endDate.substring(6, 8) +
					' 00:00:00'
				this.time2 = i
			}
		},
		toUploadFile(params) {
			uploadFileAndSort(params).then(res => {
				if (res.msgCode === 'Y') {
					this.fundManagerForm.fileId = res.obj
					this.fundManagerForm.fileName = params.file.name
				}
			})
		},
		// 文件超出个数限制时的钩子
		handleExceed(files, fileList) {
			this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
		},
		// 文件删除之前的钩子
		beforeRemove(file, fileList) {
			this.fundManagerForm.fileId = ''
			this.fundManagerForm.fileName = ''
		},
		cancel() {
			this.$emit('comIsShow')
			this.fundManagerForm = {
				fundSysCode: '',
				invest: '',
				startDate: '',
				endDate: '',
				fileId: '',
				fileName: ''
			}
			this.fileList = []
			this.checkRemark = ''
			this.time1 = ''
			this.time2 = ''
			this.time3 = ''
			this.time4 = ''
		},
		checkFundManager(state) {
			this.fundManagerForm.checkState = state
			if (state === 1) {
				checkTeam(this.fundManagerForm).then(res => {
					if (res.msgCode === 'Y') {
						this.$message({ message: res.msg, type: 'success' })
						this.$emit('queryFundManagerList')
						this.cancel()
					}
				})
			} else {
				this.$prompt('驳回原因', '提示', {
					confirmButtonText: '确定',
					concelButtonText: '取消',
					inputPattern: /[^\\s\ \r\\n]/,
					inputErrorMessage: '请输入驳回原因'
				}).then(({ value }) => {
					this.fundManagerForm.checkRemark = value
					checkTeam(this.fundManagerForm).then(res => {
						if (res.msgCode === 'Y') {
							this.$message({ message: res.msg, type: 'success' })
							this.$emit('queryFundManagerList')
							this.cancel()
						}
					})
				})
			}
		},
		// 新增或编辑
		addConfirm() {
			if (!!this.fundManagerForm.fundSysCode && this.fundManagerForm.invest && this.fundManagerForm.startDate) {
				this.fundManagerForm.startDate = this.fundManagerForm.startDate ? this.fundManagerForm.startDate : ''
				this.fundManagerForm.endDate = this.fundManagerForm.endDate ? this.fundManagerForm.endDate : ''
				if (this.title === '编辑') {
					this.fundManagerForm.checkState = '0'
					editTeam(this.fundManagerForm).then(res => {
						if (res.msgCode === 'Y') {
							this.$message({ message: res.msg, type: 'success' })
							this.$emit('queryFundManagerList')
							this.cancel()
						}
					})
				} else if (this.title === '新增') {
					addTeam(this.fundManagerForm).then(res => {
						if (res.msgCode === 'Y') {
							this.$message({ message: res.msg, type: 'success' })
							this.$emit('queryFundManagerList')
							this.cancel()
						}
					})
				}
			} else {
				this.$message({
					type: 'warning',
					message: '请填写必要参数'
				})
			}
		},
		edit(obj) {
			this.fundManagerForm = JSON.parse(obj)
			if (this.fundManagerForm.fileName) {
				this.fileList.push({ name: this.fundManagerForm.fileName })
			}
			if (this.fundManagerForm.startDate) {
				this.pickerOption1.disabledDate(new Date(this.fundManagerForm.startDate))
				this.changeTime1()
			}
			if (this.fundManagerForm.checkState === '2') {
				ramakeTeam({ checkState: '2', mainId: this.fundManagerForm.pkId }).then(res => {
					this.checkRemark = res.data.body[0].checkRemark
				})
			}
		},
		// 基金列表（下拉框）
		getFundList() {
			getFundList({}).then(res => {
				if (res.msgCode === 'Y') {
					res.data.body.forEach(item => {
						if (item.baseInfo) {
							item.fundFullName = item.baseInfo.fundFullName
							item.fundName = item.baseInfo.fundName
							item.fundCode = item.baseInfo.fundCode
						}
					})
					this.fundList = res.data.body
				}
			})
		}
	}
}
</script>
<style lang="scss" scoped src='./index.scss'>
</style>
